<?php
class Formi
{
    public $attributes = array();

    /**
     * @var Formi_Attr_Setter 
     */
    protected $attr_setter;
    protected $method;
    protected $elements = array();

    public function __construct($options=null)
    {
        $this->attr_setter = Formi_Attr_Setter::instance();

        $this->set_method($options);
        $this->attr_setter->set_element_attributes($this, $options);
    }

    /**
     * Statyczny alias do __construct()
     *
     * @param string $name
     * @param array $options
     * @return Formi
     */
    public static function factory($options=null)
    {
        return new Formi($options);
    }

    public function  __toString() {
        return (string)$this->render();
    }

    public function render()
    {
        $nform_view = View::factory('formi/form/_default')
            ->set('attributes', $this->attributes)
            ->set('method', $this->method)
            ->set('elements', $this->elements);

        return $nform_view;
    }

    private function set_method($options)
    {
        $this->method = 'POST';

        if(!is_array($options))
            return $this;

        if(array_key_exists('method', $options))
            $this->method = $options['method'];

        return $this;
    }

    public function set_class($class)
    {
        $this->attributes['class'] = $class;
    }

    public function set_id($id)
    {
        $this->attributes['id'] = $id;
    }

}